package strings;

import java.util.regex.Pattern;

public class strings {

	/** remove the last char of a string **/
	public String removeLastChar(String input)
	{
		String result = new String(input.substring(0, input.length()-1)); // remove last ,
		return result;
	}
	
	/** remove the last X chars of a string **/
	public String removeLastCharX(String input, int X)
	{
		String result = new String(input.substring(0, input.length()-X)); // remove last X
		return result;
	}
	
	/** string contains a list of elements separated by "delimiter" (e.g. ; or ,)
	 * this function converts this string to a Array of strings (String[]) that one can access in the data[index], where index
	 * is a integer number, marking the position of the String in the Array list.
	 * **/
	public String[] stringSplit2Array(String data,String delimiter)
	{
        Pattern split_actions = Pattern.compile(delimiter);
        String[] result = split_actions.split(data);
        return result;
	}
	
	/** count the number of a certain char inside a string 
	 * other words: how oftern does "x" occur in "xasdjfklx" **/
	public static int countChar(String STRING, char CHAR)
	{
		  return STRING.replaceAll("[^" + CHAR + "]","").length();
	}
	
	/** count the number of a certain char inside a string 
	 * other words: how oftern does "x" occur in "xasdjfklx" **/
	public static int countChar(String STRING, String CHAR)
	{
		return STRING.replaceAll("[^" + CHAR + "]","").length();
	}
	
	/** string (length 1) to char **/
	public static char String2Char(String STRING)
	{
    	String ChatMessageDelimiter = STRING;
    	char ChatMessageDelimiterChar = ChatMessageDelimiter.charAt(0);
    	return ChatMessageDelimiterChar;
	}
}
